package com.gemini.service.impl;

import com.gemini.mapper.UserMapper;
import com.gemini.pojo.User;
import com.gemini.service.UserService;
import com.gemini.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class UserServiceImpl implements UserService {

    private final SqlSessionFactory sqlSessionFactory=SqlSessionFactoryUtils.getSqlSessionFactory();
    @Override
    public User selectUser(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user1 = mapper.selectUser(user);
        sqlSession.close();
        return user1;
    }

    @Override
    public void addUser(User user) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addUser(user);
        sqlSession.close();
    }

    @Override
    public User selectUserByUsername(String username) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = mapper.selectUserByUsername(username);
        sqlSession.close();
        return user;
    }
}
